﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>第7章 错误和异常处理</title></head><body>
<div><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">第</span><span style="font-family: Consolas; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">7</span><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">章 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 22pt; font-weight: bold; padding-left: 49pt; line-height: 190%">错误和异常处理</span></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; padding-left: 70pt; line-height: 170%">7.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 14pt; padding-left: 70pt; line-height: 170%">异常处理的概念</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">代码在</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">try</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">代码块</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">被调用执行。若其中出现错误，可以执行一个</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">抛出异常</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">的操作。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">try</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;//code goes here</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">在</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">中，异常必须</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">手动抛出</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%">throw</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 240%">new Exception(&apos;message&apos;, code);</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">throw</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字将</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">触发</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">异常处理机制，须传递一个值，即接收一个对象（异常对象）。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">该类的构造函数需要两个参数：一个</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">错误消息</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">和</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">错误代码号</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，均为可选的。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">try</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">代码块之后必须至少给出一个</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">catch</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">代码块</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，可将多个</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">代码块与一个</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">try</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">块关联。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">catch</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">( typeint exception)</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp;//handle exception</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">传递给</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">代码块的</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">对象</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">（被</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">捕获的）就是导致异常并传递给</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">throw</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">语句的对象（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">throw</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">语句抛出）。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">异常可以是任何类型的。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">通常使用</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">Exception</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">或从</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">Exception</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">类继承过来并由用户定义的异常类实例。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">产生异常时，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">查询一个匹配的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">代码块，若有多个</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，则传递给每个</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">catch</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">的对象</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">必须具有不同的类型</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 190%">可在</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 190%">catch</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 190%">代码块产生新的异常。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; padding-left: 70pt; line-height: 170%">7.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Calibri; font-size: 18pt; padding-left: 70pt; line-height: 170%">Exception</span><span style="font-family: 宋体; font-size: 18pt; padding-left: 70pt; line-height: 170%">类</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">异常处理内置类</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">Exception</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">其构造函数，两个参数：错误消息，错误代码。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">其内置方法：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getCode()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回传递给构造函数的代码</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getMessage()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回传递给构造函数的消息。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getFile()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回产生异常代码文件的完整路径。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getLine()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回代码文件中产生异常的代码行号。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getTrace()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回一个包含了产生异常的代码回退路径的数组。</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 31pt; line-height: 190%">(</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">即发生异常时所执行的函数</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 31pt; line-height: 190%">)</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 190%">getTraceAsString()</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 31pt; line-height: 190%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">返回与</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 31pt; line-height: 190%">getTrace()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 190%">方向相同的信息，该信息将被格式化成一个字符串。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 31pt; line-height: 240%">__toString()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 240%">，允许简单地显示一个</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 31pt; line-height: 240%">Exception</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 31pt; line-height: 240%">对象，并且给出以上所有方法可以提供的信息。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">执行以下命令，可打印异常代码，消息，路径，行等简单信息。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">echo $e;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">7.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">用户自定义异常</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">可扩展</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">Exception</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">类</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">来创建自己的异常类</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可以在</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">throw</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">子句中传递任何其他对象</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__FILE__</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">, </span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">执行时所处的文件，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">$this-&gt;file = __FILE__;</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">__LINE__</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，执行时所处的行号，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">$this-&gt;line = __LINE__;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; padding-left: 70pt; line-height: 170%">7.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">Bob</span><span style="font-family: 宋体; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">的汽车零部件商店应用程序的异常</span><span style="font-family: Calibri; font-size: 18pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p151)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">文件</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">I/O</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">（或任何类型的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">I/O</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">）是程序经常出现错误的地方，应进行异常处理。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">写文件时可能的三种错误：文件无法打开、无法获得写锁或无法写入。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">良好的编码习惯要求</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">try</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">代码块的代码量较少</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">catch</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">代码块与每一个</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">instanceof</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">操作符相匹配</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">如果</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">异常没有匹配</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">的</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">catch</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">语句块，</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">PHP</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">将报告一个致命错误。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%">===================================Outline========================================================</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">1. 结构 </span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;try{ </span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new Exception( &apos;message&apos; code);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;catch ( Exception $e) { ... }</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">2. </span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 140%">Exception类</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;getCode(), get Message(), getFile(), getLine(), getTrace(), getTraceAsString(), _toString()</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>